package com.iss.supermarket.ReturnGoodsUI;

import com.iss.supermarket.util.List.ListModelArray;
import com.iss.supermarket.util.MessageWindow.MsgWin;
import com.iss.supermarket.util.entity.Detailed;
import com.iss.supermarket.util.repository.TradeDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ReturnGoodsUI extends JFrame{
    //单例模式
    private static ReturnGoodsUI returnGoodsUI;
    //定义组件
    private JLabel ReturnGoodsLabel,titleLabel;
    private JTextField ReturnGoodsTextField;
    private JButton jButton1,jButton2;
    private ReturnGoodsList returnGoodsList;
    private JPanel returnGoodsPanel,returnGoodsListPanel;

    //连接数据库
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:/spring_ioc.xml");
    TradeDao tradeDao = ctx.getBean("TradeDao", TradeDao.class);

    public ReturnGoodsUI(){
        InitAssemnly();
        InitUI();
    }

    private void InitUI(){
//        Container container = getContentPane();
        Box verticalBox = Box.createVerticalBox();

        JPanel jPanel0 =  new JPanel();
        jPanel0.add(titleLabel);
        verticalBox.add(jPanel0);

        JPanel jPanel1 = new JPanel();
        jPanel1.add(ReturnGoodsLabel);
        jPanel1.add(ReturnGoodsTextField);
        verticalBox.add(jPanel1);

        JPanel jPanel2 = new JPanel();
        jPanel2.add(jButton1);
        jPanel2.add(jButton2);
        verticalBox.add(jPanel2);

        verticalBox.add(returnGoodsListPanel);
        returnGoodsPanel.add(verticalBox);

//        container.add(verticalBox);
//        setSize(800,500);
//        setVisible(true);
    }

    private void InitAssemnly(){
        titleLabel = new JLabel("退货管理");
        titleLabel.setHorizontalAlignment(SwingConstants.LEFT);
        titleLabel.setPreferredSize(new Dimension(100,25));
        titleLabel.setFont(new Font("宋体",Font.BOLD,20));

        ReturnGoodsLabel = new JLabel("交易编码");
        ReturnGoodsLabel.setHorizontalAlignment(SwingConstants.LEFT);
        ReturnGoodsLabel.setPreferredSize(new Dimension(80,25));
        ReturnGoodsLabel.setFont(new Font("黑体",Font.PLAIN,15));

        ReturnGoodsTextField = new JTextField();
        ReturnGoodsTextField.setHorizontalAlignment(SwingConstants.CENTER);
        ReturnGoodsTextField.setPreferredSize(new Dimension(300,25));
        ReturnGoodsTextField.setFont(new Font("黑体",Font.PLAIN,15));

        jButton1 = new JButton("查询商品交易记录");
        jButton1.addActionListener(new ButtonAction());
        jButton1.setFont(new Font("黑体",Font.PLAIN,14));
        jButton2 = new JButton("确定退货");
        jButton2.addActionListener(new ButtonAction());
        jButton2.setFont(new Font("黑体",Font.PLAIN,14));

        returnGoodsList = ReturnGoodsList.getReturnGoodsList();
        returnGoodsListPanel = returnGoodsList.getReturnGoodsListPanel();

        returnGoodsPanel = new JPanel();
    }

    private class ButtonAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if("查询商品交易记录".equals(e.getActionCommand())){
                if(!"".equals(ReturnGoodsTextField.getText())){
                    Detailed detailed = tradeDao.queryDetailedInfoByUUID(ReturnGoodsTextField.getText());
                    returnGoodsList.flush(detailed);
                }else{
                    MsgWin msgWin = new MsgWin("查询商品交易失败");
                }
            }else if("确定退货".equals(e.getActionCommand())){
                //确定退货后，清空前一次的退货信息
                ReturnGoodsTextField.setText("");

                //退货信息插入数据库中
                ListModelArray<Detailed> listModelArray = returnGoodsList.getListModelArray();
                for(int i=0;i<listModelArray.size();++i){
                    Detailed tmpdetailed = listModelArray.getElementAt(i);
                    tradeDao.ReturnGoods(tmpdetailed.getGdBarCode(),
                            tmpdetailed.getTrUuid(),
                            tmpdetailed.getGdBroadName(),
                            tmpdetailed.getDtgdNum(),
                            tmpdetailed.getDtgdPrice(),
                            tmpdetailed.getModetime());
                }
                //确定退货后清空退货信息表
                returnGoodsList.Clear();
                //完成退货，返回提示信息
                MsgWin msgWin = new MsgWin("已退货");
            }
        }
    }

    public static synchronized ReturnGoodsUI getReturnGoodsUI(){
        if(returnGoodsUI == null){
            returnGoodsUI = new ReturnGoodsUI();
        }
        return returnGoodsUI;
    }

    public JPanel getReturnGoodsPanel() {
        return returnGoodsPanel;
    }
}
